package ControlLayer;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

public class CustomerCtr
{
    public CustomerCtr()
    {}
    
    public ArrayList<Customer> getAllCustomers()
    {
    	DBCustomer dbCust = new DBCustomer();
    	ArrayList<Customer> allCust = new ArrayList<>();
    	allCust = dbCust.getAllCustomers(false);
    	return allCust;
    }
    
    public Customer getCustomerByName(String name)
    {
    	DBCustomer dbCust = new DBCustomer();
        return dbCust.getCustomerByName(name, false);
    }
    
    public Customer getCustomerByID(int id)
    {
    	DBCustomer dbCust = new DBCustomer();
    	return dbCust.getCustomerByID(id, false);
    }
    
    public void updateCustomer(int newCustomerID, String newCustomerName, String newAddress, int newzipCode, String newCity, String newPhone, String newEmail)
    {
    	IFDBCustomer dbCust=new DBCustomer();
    	
    	Customer cust = new Customer();
    	cust.setAddress(newAddress);
    	cust.setCity(newCity);
    	cust.setID(newCustomerID);
    	cust.setName(newCustomerName);
    	cust.setEmail(newEmail);
    	cust.setPhone(newPhone);
    	cust.setzipCode(newzipCode);
    	
    	dbCust.updateCustomer(cust);
    }
    
    public void insertCustomer(String newCustomerName, String newAddress, int newzipCode, String newCity, String newPhone, String newEmail)
    {
    	Customer custObj=new Customer();
    	custObj.setName(newCustomerName);
    	custObj.setAddress(newAddress);
    	custObj.setzipCode(newzipCode);
    	custObj.setCity(newCity);
    	custObj.setPhone(newPhone);
    	custObj.setEmail(newEmail);
    	try
 		 {
 			 DBConnection1.startTransaction();
 			 DBCustomer dbCust=new DBCustomer();
 			 dbCust.insertCustomer(custObj);
 			 DBConnection1.commitTransaction();
 		 }
 		 catch(Exception e)
 		 {
 			 DBConnection1.rollbackTransaction();
 		 }
      }

      public int deleteCustomerByID(int id)
      {
    	  IFDBCustomer dbCust = new DBCustomer();
    	  return dbCust.deleteCustomer(id);
      }
      
      public ArrayList<SalesOrder> findCustomerOrders(int customerId)
      {
    	  IFDBSalesOrder dbsalesOrder = new DBSalesOrder();
    	  ArrayList<SalesOrder> custOrders = new ArrayList<>();
    	  custOrders = dbsalesOrder.getCustomerSalesOrders(customerId, true);
    	  return custOrders;
      }
}

